import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class TextArea extends StatefulWidget {
  const TextArea({
    super.key,
    this.controller,
    this.hintText,
    this.minLines = 4,
    this.maxLines,
    this.maxLength,
    this.autofocus = false,
  });

  final TextEditingController? controller;
  final String? hintText;
  final int minLines;
  final int? maxLines;
  final int? maxLength;
  final bool autofocus;

  @override
  State<TextArea> createState() => _TextAreaState();
}

class _TextAreaState extends State<TextArea> {
  late final TextEditingController _controller;
  int _length = 0;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();
    _length = _controller.text.characters.length;
    _controller.addListener(_onTextChanged);
  }

  void _onTextChanged() {
    setState(() {
      _length = _controller.text.characters.length;
    });
  }

  @override
  void dispose() {
    if (widget.controller == null) {
      _controller.dispose();
    } else {
      _controller.removeListener(_onTextChanged);
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final int? maxLen = widget.maxLength;

    return Material(
      color: Colors.transparent,
      child: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
              color: Colors.grey.shade100,
              borderRadius: BorderRadius.circular(12),
            ),
            padding: const EdgeInsets.fromLTRB(14, 10, 14, 30),
            child: TextField(
              controller: _controller,
              autofocus: widget.autofocus,
              minLines: widget.minLines,
              maxLines: widget.maxLines ?? 20,
              maxLength: maxLen,
              maxLengthEnforcement:
                  maxLen != null ? MaxLengthEnforcement.enforced : null,
              keyboardType: TextInputType.multiline,
              style: theme.textTheme.bodyLarge,
              decoration: InputDecoration(
                isDense: true,
                border: InputBorder.none,
                hintText: widget.hintText ?? '请输入内容',
                hintStyle: theme.textTheme.bodyLarge?.copyWith(
                  color: Colors.grey.shade500,
                ),
                counterText: '', // 用自定义计数器
              ),
            ),
          ),
          // 底部右侧计数器和可提升按钮
          Positioned(
            right: 10,
            bottom: 6,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                // 使用数 / 最大数（如果提供）
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                  decoration: BoxDecoration(
                    color: Colors.transparent,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    maxLen == null ? '$_length' : '$_length / $maxLen',
                    style: theme.textTheme.bodySmall?.copyWith(
                      color: Colors.grey.shade600,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
